define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');


    var firstTime = true;

    var itemCat = {
        count: 0,
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "商品叶子类目关联规则") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initTable();
            this.initDatetimepicker();
            this.bindEvent();
        },
        /**
         * 执行查询操作.
         */
        doSearch: function () {
            $('#table').DataTable().ajax.reload();
        },
        /**
         * 初始化列表
         */
        initTable: function () {
            var self = this;
            $("#table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": true,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": true, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/iris-api/service'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (firstTime) {
                        firstTime = false;
                        return;
                    }
                    var dataString, _data = {};
                    $($("#J_Search").serializeArray()).each(function () {
                        _data[this.name] = this.value;
                    });
                    _data.pageNo = aoData[3].value;
                    _data.pageSize = aoData[4].value;
                    var param = {};
                    param.param = _data;
                    param.serviceName = "associateCate";
                    dataString = JSON.stringify(param);
                    self.initCount();
                    CRM.request({
                        "type": 'POST',
                        "url": sSource,
                        "dataType": "json",
                        contentType: "application/json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": dataString,
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = self.count;
                                    resultData.iTotalRecords = self.count;
                                    $.each(req.entry.result, function (key, entry) {
                                        resultData.aaData.push(entry)
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                },
                "aaSorting": [[1, "asc"]],  //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "前项叶子类目id",
                        "mDataProp": "pre_cate_id",
                        "bSortable": true
                    },
                    {
                        "sTitle": "前项叶子类目名称",
                        "mDataProp": "pre_cate_name",
                        "bSortable": true
                    },
                    {
                        "sTitle": "后项叶子类目id",
                        "mDataProp": "post_cate_id",
                        "bSortable": true
                    },
                    {
                        "sTitle": "后项叶子类目名称",
                        "mDataProp": "post_cate_name",
                        "bSortable": true
                    },
                    {
                        "sTitle": "支持度",
                        "mDataProp": "support",
                        "bSortable": true
                    },
                    {
                        "sTitle": "置信度",
                        "mDataProp": "confidence",
                        "bSortable": true
                    },
                    {
                        "sTitle": "提升度",
                        "mDataProp": "lift",
                        "bSortable": true
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "full_numbers" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 初始化日期控件
         */
        initDatetimepicker: function () {
            $(".form_time").datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 1,
                minView: 0,
                maxView: 1,
                forceParse: 0
            });
            $('.form_date').datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });
        },
        /**
         * 初始化数量
         */
        initCount: function () {
            var self = this;
            var dataString, _data = {};
            $($("#J_Search").serializeArray()).each(function () {
                _data[this.name] = this.value;
            });
            var param = {};
            param.param = _data;
            param.serviceName = "associateCateCnt";
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl('/iris-api/service'),
                "dataType": "json",
                async: false, //请求是否异步，默认为异步，这也是ajax重要特性
                contentType: "application/json",
                xhrFields: {
                    withCredentials: true
                },
                "data": JSON.stringify(param),
                "success": function (req) {
                    if (req.status) {
                        if (req.entry) {
                            self.count = req.entry.result[0].cnt
                        } else {
                            CRM.showDialog(req.message);
                        }
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
        },
        /**
         * 绑定事件
         */
        bindEvent: function () {
            var self = this;
            $("#search_btn").on('click', function () {
                self.doSearch();
            });

        }
    };

    window.doSearch = $.proxy(itemCat.doSearch, itemCat);

    itemCat.init();
});